<!-- 点击试卷后的缩略信息,我的考试 -->
<template>
  <div id="msg">
    <div class="title">
      <span></span>
      <span>试卷/ {{examData.source}}</span>
    </div>
    <div class="wrapper">
      <ul class="top">
        <li class="example">{{examData.source}}</li>
        <li><i class="iconfont icon-pen-"></i></li>
        <li><i class="iconfont icon-share"></i></li>
        <li class="right">
          <div>
            <span class="count">总分</span>
            <span class="score">{{examData.totalScore}}</span>
          </div>
        </li>
      </ul>
      <ul class="bottom">
        <li>更新于{{examData.examDate}}</li>
        <li>来自 {{examData.institute}}</li>
        <li class="btn">{{examData.examType}}</li>
        <li class="right"><el-button @click="toAnswer(examData.examCode)">开始答题</el-button></li>
      </ul>
      <ul class="info">
        <li @click="dialogVisible = true"><a href="javascript:;"><i class="iconfont icon-info"></i>考生须知</a></li>
      </ul>
    </div>
    <div class="content">
      <el-collapse v-model="activeName" >
        <el-collapse-item class="header" name="0">
        <template #title>
          <div class="title stitle">
            <span>{{ examData.source }}</span>
            <i class="header-icon el-icon-info"></i>
            <span class="time">{{ examData.totalScore }}分 / {{ examData.totalTime }}分钟</span>
            <el-button type="primary" size="small">点击查看试题详情</el-button>
          </div>
        </template>
          <el-collapse class="inner">
            <el-collapse-item>
              <template #title name="1">
                <div class="titlei">选择题 (共{{examData.paperDetail[0]}}题 共计{{score[0]}}分)</div>
              </template>
              <div class="contenti">
                <ul class="question" v-for="(list, index) in topic[1]" :key="index">
                  <li>{{index+1}}. {{list.question}} {{list.score}}分</li>
                </ul>
              </div>
            </el-collapse-item>
            <el-collapse-item>
              <template #title name="2">
                <div class="titlei">填空题 (共{{examData.paperDetail[1]}}题  共计{{score[1]}}分)</div>
              </template>
              <div class="contenti">
                <ul class="question" v-for="(list, index) in topic[2]" :key="index">
                  <li>{{examData.paperDetail[0]+index+1}}.{{list.question}}  {{list.score}}分</li>
                </ul>
              </div>
            </el-collapse-item>
            <el-collapse-item>
              <template #title name="3">
                <div class="titlei">判断题 (共{{examData.paperDetail[2]}}题 共计{{score[2]}}分)</div>
              </template>
              <div class="contenti">
                <ul class="question" v-for="(list, index) in topic[3]" :key="index">
                  <li>{{examData.paperDetail[0]+examData.paperDetail[1]+index+1}}. {{list.question}} {{list.score}}分</li>
                </ul>
              </div>
            </el-collapse-item>
          </el-collapse>
        </el-collapse-item>

      </el-collapse>
    </div>
    <!--考生须知对话框-->
    <el-dialog
      title="考生须知"
      :visible.sync="dialogVisible"
      width="30%">
      <span>{{examData.tips}}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">知道了</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import {getExam,getPaperQuestionList} from '@/api/module/exam'

// 路由相关
const route = useRoute();
const router = useRouter();

// 响应式数据
const dialogVisible = ref(false);
const activeName = ref(['0']); // 默认打开第一个折叠项
const score = ref([]); // 每种试题类型分数的总数
const examData = ref({
  source: '',
  totalScore: 0,
  totalTime: 0,
  examDate: '',
  institute: '',
  examtype: '',
  tips: '',
  examCode: '',
  paperId: 0,
  paperDetail: [],
});
//题目列表
const questionList=ref([])


// 计算总分
const totalScore = computed(() => {
  return score.value.reduce((sum, current) => sum + current, 0);
});

const judgeQuestionList=computed(()=>{
  //这里取questionList的前exam.value.paperDetail[0]条数据
    // 为空保护
    if (!questionList.value || questionList.value.length === 0) return [];
    if (!examData.value.paperDetail || examData.value.paperDetail.length < 1) return [];
    return questionList?.value.slice(0, examData.value.paperDetail[0]);
})
const multiQuestionList=computed(()=>{
    // 为空保护
    if (!questionList.value || questionList.value.length === 0) return [];
    if (!examData.value.paperDetail || examData.value.paperDetail.length < 1) return [];
  const start = examData.value.paperDetail[0];
  const end = start + examData.value.paperDetail[1];
  return questionList?.value.slice(start, end);
})
const fillQuestionList=computed(()=>{ 
  // 为空保护
    if (!questionList.value || questionList.value.length === 0) return [];
    if (!examData.value.paperDetail || examData.value.paperDetail.length < 1) return [];
  const start = examData.value.paperDetail[0] + examData.value.paperDetail[1];
  return questionList?.value.slice(start);
})
const topic=computed(()=>{
  return {
  '1': judgeQuestionList.value,
  '2': multiQuestionList.value,
  '3': fillQuestionList.value
}
})
    // 获取考试信息
const getExamData = async () => { 
    await  getExam( route.query.examCode).then(res=>{
      examData.value=res.data
     })
}
     //获取试卷试题(数组)
const getQuestion=async ()=>{
    await getPaperQuestionList({examCode:route.query.examCode}).then(res=>{
      questionList.value=res.rows
     })
}
// 初始化页面数据
const init = async () => {  
     await Promise.all([
      getExamData(),
      getQuestion()
    ]);
  try {
    // 计算题型数量和分数
    Object.values(topic.value).forEach(data => {
      //题型的总分
      let currentScore = 0;
      for (let i = 0; i < data.length; i++) {
        currentScore += data[i].score;
      }
      score.value.push(currentScore);
});
  } catch (error) {
    console.error('初始化数据失败:', error);
  }
};

// 跳转到答题页面
const toAnswer = (id) => {
  router.push({ path: "/answering", query: { examCode: route.query.examCode } });
};

// 生命周期钩子
onMounted(() => {
  init();
});
</script>

<style lang="less" scoped>
.bottom {
  .right{
    .el-button{
      color: #409EFF;
      border-color: #c6e2ff;
      background-color: #ecf5ff;
    }
  }
}
.right {
  margin-left: auto;
}
.inner .contenti .question {
  margin-left: 40px;
  color: #9a9a9a;
  font-size: 14px;
}

.content .inner .titlei {
  margin-left: 20px;
  font-size: 16px;
  color: #88949b;
  font-weight: bold;
}
.content .title .time {
  font-size: 16px;
  margin-left: 420px;
  color: #999;
}

.content .title span {
  margin-right: 10px;
}
#msg .content .title {
  font-size: 20px;
  margin: 0px;
  display: flex;
  align-items: center;
  color:#646464
}
.content {
  margin-top: 20px;
  background-color: #fff;
  // color: #333;
}
.content .header {
  padding: 10px 30px;
}
.wrapper .info {
  margin: 20px 0px 0px 20px;
  border-top: 1px solid #eee;
  padding: 20px 0px 10px 0px;
}
.wrapper .info a {
  color: #88949b;
  font-size: 14px;
}
.wrapper .info a:hover {
  color: #0195ff;
}
.wrapper .bottom .btn {
  cursor: pointer;
  padding: 5px 10px;
  border: 1px solid #88949b;
  border-radius: 4px;
}
.wrapper .bottom {
  display: flex;
  margin-left: 20px;
  color: #999;
  font-size: 14px;
  align-items: center;
}
.wrapper .bottom li {
  margin-right: 14px;
    list-style-type: none;

}
#msg {
  background-color: #eee;
  width: 980px;
  margin: 0 auto;
}
#msg .title {
  margin: 20px;
}
#msg .wrapper {
  background-color: #fff;
  padding: 10px;
}
.wrapper .top {
  display: flex;
  margin: 20px;
  align-items: center;
}
.wrapper .top .right {
  margin-left: auto;
      list-style-type: none;

}
.wrapper .top .example {
  color: #333;
  font-size: 22px;
  font-weight: 700;
    list-style-type: none;
  

}
 .example li {
          list-style-type: none;

    }
.wrapper .top li i {
  margin-left: 20px;
  color: #88949b;
}
.wrapper .right .count {
  margin-right: 60px;
  color: #fff;
  padding: 4px 10px;
  background-color: #88949b;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border: 1px solid #88949b;
}
.wrapper .right .score {
  position: absolute;
  left: 53px;
  top: -5px;
  padding: 1px 12px;
  font-size: 20px;
  color: #88949b;
  border: 1px dashed #88949b;
  border-left: none;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  font-weight: bold;
}
.wrapper .right div {
  position: relative;
}
</style>
